# https://blog.csdn.net/weixin_41711422/article/details/105460138?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
# 用keras框训练猫狗分类并进行验证

#将VGG16卷积基实例化
from tensorflow.keras.applications import VGG16
conv_base = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))

# 创建数据集
import os,shutil
oirginal_dataset_dir='C:/Users/45005/.keras/datasets/kaggle_original_data/train'
base_dir='C:/Users/45005/.keras/datasets/kaggle_original_data/test2'
os.mkdir(base_dir)#创建文件夹C:/Users/45005/.keras/datasets/kaggle_original_data/test2

train_dir=os.path.join(base_dir,'train')#分别对应划分后的训练，验证和测试的目录
#os.mkdir(train_dir)
validation_dir=os.path.join(base_dir,'validation')
#os.mkdir(validation_dir)
test_dir=os.path.join(base_dir,'test')
#os.mkdir(test_dir)
#训练目录
train_cats_dir=os.path.join(train_dir,'cats')#猫的训练图像目录
#os.mkdir(train_cats_dir)
train_dogs_dir=os.path.join(train_dir,'dogs')#狗的训练图像目录
#os.mkdir(train_dogs_dir)
#验证目录
validation_cats_dir=os.path.join(validation_dir,'cats')#猫的验证图像目录
#os.mkdir(validation_cats_dir)
validation_dogs_dir=os.path.join(validation_dir,'dogs')#狗的验证图像目录
#os.mkdir(validation_dogs_dir)
#测试目录
test_cats_dir=os.path.join(test_dir,'cats')#猫的测试图像目录
#os.mkdir(test_cats_dir)
test_dogs_dir=os.path.join(test_dir,'dogs')#狗的测试图像目录
#os.mkdir(test_dogs_dir)
#将前1000张的猫的图像复制到train_cats_dir中
fnames=['cat.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(train_cats_dir,fname)
   shutil.copyfile(src,dst)
   #将接下来500张猫复制到validation_cat目录中
fnames=['cat.{}.jpg'.format(i) for i in range(1000,1500)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(validation_cats_dir,fname)
   shutil.copyfile(src,dst)
#将接下来500张猫复制到test_cat中
fnames=['cat.{}.jpg'.format(i) for i in range(1500,2000)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(test_cats_dir,fname)
   shutil.copyfile(src,dst)
#和前面一样的操作，将前1000张的狗狗的图像复制到train_cats_dir中
fnames=['dog.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(train_dogs_dir,fname)
   shutil.copyfile(src,dst)
   #将接下来500张狗狗复制到validation_cat
fnames=['dog.{}.jpg'.format(i) for i in range(1000,1500)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(validation_dogs_dir,fname)
   shutil.copyfile(src,dst)
#将接下来500张狗狗复制到test_dog中
fnames=['dog.{}.jpg'.format(i) for i in range(1500,2000)]
for fname in fnames:
   src=os.path.join(oirginal_dataset_dir,fname)
   dst=os.path.join(test_dogs_dir,fname)
   shutil.copyfile(src,dst)
#验证 train,validation,test集中是否和设置一样
print('total training cat images:',len(os.listdir(train_cats_dir)))
print('total training dog images:',len(os.listdir(train_dogs_dir)))
print('total validation cat images:',len(os.listdir(validation_cats_dir)))
print('total validation dog images:',len(os.listdir(validation_cats_dir)))
print('total test cat images:',len(os.listdir(test_cats_dir)))
print('total test dog images:',len(os.listdir(test_dogs_dir)))
